package com.client.koshercook.client;



import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;

public class LoginPanel extends Composite {
	
	private HorizontalPanel HPanel = new HorizontalPanel();
	private EdgesPanel edgesPanel = new EdgesPanel(HPanel);
	private Image logo = new Image("logo_3.png");
	private Image loginPButton = new Image();
	private Fridge fridge;
	private UserHistory userHistory = new UserHistory();
	private UserFavorites userFavorites = new UserFavorites();
	private AddRecipeBox addRecipeBox = new AddRecipeBox();
	private String buttonWidht;
	private HTML userNick = new HTML("שלום אורח");
	
	private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
	
	public LoginPanel(Fridge fridge, int windowWidht){
		this.fridge = fridge;
		userNick.setDirection(Direction.RTL);
		userNick.setStyleName("generalInfo3");

		if (windowWidht <= 800)
			buttonWidht = "40px";
		else
			buttonWidht = "132px";
		
		HPanel.setSpacing(10);
		
		HPanel.add(logo);
		HPanel.add(addRecipeBox);
		HPanel.add(userHistory);
		HPanel.add(userFavorites);
		HPanel.add(fridge);
				
		//check if the user is connected
		greetingService.isUser(new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
			}
			public void onSuccess(String result) {		
				if (!result.equals("no")){ //connected
					loginPButton = newDisconnectButton(buttonWidht,"24px");
					Client.model.setUserNick(result);
					userNick.setText("שלום"+" "+result);
				}
				else{	//not connected
					loginPButton = newConnectButton(buttonWidht,"24px");
					Client.model.setConnected(false);
				}
				HPanel.add(loginPButton);
				HPanel.add(userNick);
			}
		});
		initWidget(edgesPanel);
	}


	public static native String getUrl()/*-{
    	return $wnd.location;
	}-*/;

	public static native void redirect(String url)/*-{
      $wnd.location = url;
  	}-*/;
	
	public void setSmaller(){
		addRecipeBox.setWidth("50px");
		userHistory.setWidth("50px");
		userFavorites.setWidth("55px");
		fridge.setWidth("50px");	
		logo.setUrl("logo_3_small.png");

	}
	
	public Image newConnectButton(String width,String height){
		Image b = new Image();

		b.setUrl("connect_off.gif");
		b.setSize(width, height);
				
		b.addMouseOverHandler(new MouseOverHandler(){
			public void onMouseOver(MouseOverEvent event) {
				loginPButton.setUrl("connect_on.gif");
			}
		});
		b.addMouseOutHandler(new MouseOutHandler(){
			public void onMouseOut(MouseOutEvent event) {
				loginPButton.setUrl("connect_off.gif");
			}
		});
		
		b.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				greetingService.logIn(new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(String result) {
						if (result != null){
							redirect(result);
						}
					}
				});
			}
		});
		
		return b;
	}
	
	public Image newDisconnectButton(String width,String height){
		Image b = new Image();

		b.setUrl("disconnect_off.gif");
		b.setSize(width, height);
		
		b.addMouseOverHandler(new MouseOverHandler(){
			public void onMouseOver(MouseOverEvent event) {
				loginPButton.setUrl("disconnect_on.gif");
			}
		});
		b.addMouseOutHandler(new MouseOutHandler(){
			public void onMouseOut(MouseOutEvent event) {
				loginPButton.setUrl("disconnect_off.gif");
			}
		});
		
		b.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				greetingService.logOut(new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
					}
					public void onSuccess(String result) {
						if (result != null){
							redirect(result);
						}
					}
				});
			}
		});
		
		return b;
	}
}
